﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Search
{
    public class SearchManager
    {

        public List<ObjectText> objectText { get; set; }

        public SearchManager()
        {
            //lam moi du lieu
            objectText = new List<ObjectText>();
        }

        public List<string> GoiY(string input,bool enter = false)
        {
            string tam = input;
            List<string> goiy = new List<string>();
            if (input.Length >= 2)
            {
                //tu nhap vao phai lon hon 2 ky tu
                int size = 0;
                if (objectText.Count != 0)
                {
                    size = objectText.Max(x => x.endIndex);
                }
                if (input.Length >= size)
                {
                    //neu viet them ky tu
                    //tach du lieu nhap moi ra khoi du lieu da dinh nghia
                    tam = input.Substring(size);
                    if (string.IsNullOrEmpty(tam.Trim()))
                    {
                        //neu khong co tu nao duoc nhao vao
                        if (objectText.Count > 0)
                        {
                            var textLast = objectText.LastOrDefault().text;
                            //neu co tu da duoc dinh nghia thi goi y theo tu do
                            goiy.AddRange(GoiYTinh(textLast));//toi da 5 goi y uu tien theo diem
                            goiy.AddRange(GoiYHuyen(textLast));//toi da 5 goi y uu tien theo diem
                        }
                    }
                    else
                    {
                        //neu co tu thi goi y theo tu do
                        goiy.AddRange(GoiYTinh(tam));//toi da 5 goi y uu tien theo diem
                        goiy.AddRange(GoiYHuyen(tam));//toi da 5 goi y uu tien theo diem
                        if (goiy.Count == 0 || enter)
                        {
                            //lay ly ky tu truoc no
                            var space = tam.Substring(tam.Length - 2, 1).Trim();
                            //neu ko co goi y nao
                            if (string.IsNullOrEmpty(space) || enter)
                            {
                                //neu truoc tu do la dau cach
                                //thi lay tu thua truoc no de goi y
                                var debug = tam.Substring(0, tam.Length - 2).Trim();
                                var leng = input.Length - 2;
                                if (enter)
                                {
                                    debug = tam.Trim();
                                    leng = input.Length;
                                }
                                goiy.AddRange(GoiYTinh(debug));//toi da 5 goi y uu tien theo diem
                                goiy.AddRange(GoiYHuyen(debug));//toi da 5 goi y uu tien theo diem

                                if (GoiYTinh(debug).Count > 0)
                                {
                                    var data = goiy.FirstOrDefault(x => x == debug);
                                    if (data != null)
                                    {
                                        //da nhan dang duoc 1 tu
                                        objectText.Add(new ObjectText
                                        {
                                            text = debug,
                                            type = 0,
                                            endIndex = leng
                                        });
                                        return GoiY(input);
                                    }
                                }

                                if (GoiYHuyen(debug).Count > 0)
                                {
                                    var data = goiy.FirstOrDefault(x => x == debug);
                                    if (data != null)
                                    {
                                        //da nhan dang duoc 1 tu
                                        objectText.Add(new ObjectText
                                        {
                                            text = debug,
                                            type = 1,
                                            endIndex = input.Length - 2
                                        });
                                        return GoiY(input);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    //neu xoa chu
                    objectText.Remove(objectText.LastOrDefault());
                    return GoiY(input);
                }
            }
            return goiy;
        }


        private List<string> GoiYTinh(string input)
        {
            List<string> goiy = new List<string>();
            if (!objectText.Any(x=>x.type == 0))
            {
                input = input.Trim().ToLower();     
                City dataCity = new City();
                var data = dataCity.items.Where(x => x.key.Contains(input)).Select(x => x.key).ToList();
                goiy.AddRange(data);
            }
            return goiy;
        }

        private List<string> GoiYHuyen(string input)
        {
            List<string> goiy = new List<string>();
            if (!objectText.Any(x => x.type == 1))
            {
                input = input.Trim();
                District dataCity = new District();
                var data = dataCity.items.Where(x => x.key.Contains(input)).Select(x => x.key).ToList();
                goiy.AddRange(data);
            }
            return goiy;
        }

        private List<string> GoiYDanhMuc(string input)
        {
            List<string> goiy = new List<string>();
            return goiy;
        }
    }

    public class ObjectText
    {
        public string text { get; set; }

        public int endIndex { get; set; }

        /// <summary>
        ///| 0: thanh pho | 1 la quan | 2 la danh muc | 3 mon an |
        /// </summary>
        public int type { get; set; }
    }
}
